﻿using MovingChame.WindowsStoreApp.Common;
using MovingChame.WindowsStoreApp.ViewModels;
using MovingChameLibrary.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.System.Threading;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MovingChame.WindowsStoreApp.UIModule
{
    public sealed partial class AdvancedSearchPage : LayoutAwarePage
    {
        private const string HideCriteria = "Hide Criteria";
        private const string ShowCriteria = "Show Criteria";

        private AdvancedSearchPageViewModel _viewModel;
        public AdvancedSearchPageViewModel ViewModel
        {
            get { return this._viewModel; }
            set
            {
                this._viewModel = value;
                this.DataContext = value;
            }
        }

        public AdvancedSearchPage()
        {
            this.InitializeComponent();
            CriteriaButton.Content = HideCriteria;
        }

        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (pageState != null && pageState.ContainsKey("SearchResult"))
            {
                ViewModel = pageState["SearchResult"] as AdvancedSearchPageViewModel;
                ResultHeader.Visibility = Visibility.Visible;
            }
            else
                ViewModel = new AdvancedSearchPageViewModel();
        }

        protected override void SaveState(Dictionary<String, Object> pageState)
        {
            pageState["SearchResult"] = ViewModel;
        }

        private void ResultItem_Click(object sender, ItemClickEventArgs e)
        {
            Tour tour = (Tour)e.ClickedItem;
            this.Frame.Navigate(typeof(TourOverviewPage), tour.Id);
        }

        private void CriteriaButton_Click(object sender, RoutedEventArgs e)
        {
            if (Criteria.Visibility == Visibility.Visible)
            {
                CriteriaButton.Content = ShowCriteria;
                bool result = VisualStateManager.GoToState(this, "Closed", true);
            }
            else if (Criteria.Visibility == Visibility.Collapsed)
            {
                CriteriaButton.Content = HideCriteria;
                bool result = VisualStateManager.GoToState(this, "Opened", true);
            }
        }
    }
}